package com.lineage.server.command.executor;

import com.lineage.server.datatables.SkillsTable;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.skill.L1SkillUse;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Skills;
import com.lineage.server.world.World;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1Buff implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1Buff.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1Buff();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      StringTokenizer tok = new StringTokenizer(arg);
      Collection<L1PcInstance> players = null;
      String s = tok.nextToken();
      if (s.equals("me")) {
        players = new ArrayList<>();
        players.add(pc);
        s = tok.nextToken();
      } else if (s.equals("all")) {
        players = World.get().getAllPlayers();
        s = tok.nextToken();
      } else {
        players = World.get().getVisiblePlayer((L1Object)pc);
      } 
      int skillId = Integer.parseInt(s);
      int time = 0;
      if (tok.hasMoreTokens())
        time = Integer.parseInt(tok.nextToken()); 
      L1Skills skill = SkillsTable.get().getTemplate(skillId);
      if (skill.getTarget().equals("buff")) {
        Iterator<L1PcInstance> iterator = players.iterator();
        while (iterator.hasNext()) {
          L1PcInstance tg = iterator.next();
          (new L1SkillUse()).handleCommands(pc, skillId, tg.getId(), tg.getX(), tg.getY(), time, 2);
        } 
      } else if (skill.getTarget().equals("none")) {
        Iterator<L1PcInstance> iterator2 = players.iterator();
        while (iterator2.hasNext()) {
          L1PcInstance tg = iterator2.next();
          (new L1SkillUse()).handleCommands(tg, skillId, tg.getId(), tg.getX(), tg.getY(), time, 4);
        } 
      } else {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("buff系のスキルではありません。"));
      } 
    } catch (Exception e) {
      _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
    } 
  }
}
